package model

import (
	"gitee.com/ling1314/origin-utility/utils"
	"gorm.io/plugin/soft_delete"
)

const ()

const (
	RootId = 1

	UserStatusActivated = 1 // 可用
	UserStatusDisabled  = 2 // 禁用

	UserOnline  = 1 // 在线
	UserOffline = 2 // 离线

	AccountLvForRoot  = 1 // 超级管理员
	AccountLvForAdmin = 2 //管理员
	AccountLvForUser  = 3 //用户
)

var RootUser = &User{
	UserId:    RootId,
	Username:  "root",
	Password:  "3.14",
	Status:    UserStatusActivated,
	IsOnline:  UserOffline,
	AccountLv: AccountLvForRoot,
	DeptId:    DeptIdForRoot,
	Creator:   "root",
	CreatorId: RootId,
	Extend: UserExtend{
		Id:       RootId,
		UserId:   RootId,
		Nickname: "root",
		Email:    "337815986@qq.com",
	},
}

type User struct {
	UserId    int64                 `json:"userId,string" gorm:"primaryKey"`
	Username  string                `json:"username" gorm:"comment:'账号'"`
	Password  string                `json:"password" gorm:"comment:'密码'"`
	Status    int                   `json:"status" gorm:"comment:'账户状态, 1-正常；2-停用'"`
	IsOnline  int                   `json:"isOnline" gorm:"comment:'是否在线'"`
	AccountLv int                   `json:"accountLv"`
	DeptId    int64                 `json:"deptId"`
	Creator   string                `json:"creator" gorm:"comment:'创建者'"`
	CreatorId int64                 `json:"creatorId,string" gorm:"comment:'创建者id'"`
	CreatedAt int64                 `json:"createdAt,string" gorm:"autoCreateTime"`
	UpdatedAt int64                 `json:"updatedAt,string" gorm:"autoUpdateTime"`
	DeletedAt soft_delete.DeletedAt `json:"-"`
	Extend    UserExtend            `json:"extend,omitempty" gorm:"foreignKey:UserId"`
}

func (r *User) TableName() string {
	return "sys_user"
}

type MapUserRole struct {
	UserId int64 `json:"userId,string"`
	RoleId int64 `json:"roleId,string"`
}

func (r *MapUserRole) TableName() string {
	return "sys_map_user_role"
}

type LoginAccount struct {
	Username string `json:"username" form:"username"`
	Password string `json:"password" form:"password"`
	VeryCode
}

type VeryCode struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type ChangePassword struct {
	NewPassword string `json:"newPassword"`
	OldPassword string `json:"oldPassword"`
}

type LoginSuccess struct {
	Username     string   `json:"username"`
	Roles        []string `json:"roles"`
	AccessToken  string   `json:"accessToken"`
	RefreshToken string   `json:"refreshToken"`
	Expires      int64    `json:"expires"`
}

type SearchUser struct {
	Username string `json:"username" form:"username"`
}

type UserPage struct {
	User  User   `json:"user"`
	Dept  Dept   `json:"dept"`
	Roles []Role `json:"roles"`
}

type UserDetails struct {
	UserInfo *User  `json:"userInfo"`
	Dept     Dept   `json:"dept"`
	Roles    []Role `json:"roles"`
}

type UserForm struct {
	UserId    int64                  `json:"userId,string"`
	Username  string                 `json:"username"`
	Password  string                 `json:"password"`
	Status    int                    `json:"status"`
	DeptId    int64                  `json:"deptId,string"`
	Nickname  string                 `json:"nickname" gorm:"comment:'昵称'"`
	Email     string                 `json:"email" gorm:"comment:'邮箱'"`
	AccountLv int                    `json:"accountLv"`
	RoleIds   utils.StringArrayInt64 `json:"roleIds"`
}

func (u *UserForm) Extend() UserExtend {
	return UserExtend{
		Nickname: u.Nickname,
		Email:    u.Email,
	}
}
